package com.piano.moguyun;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.piano.moguyun.act.SignAct;
import com.piano.moguyun.bean.LoginBean;
import com.piano.moguyun.bean.PlanByStuBean;
import com.piano.moguyun.bean.SendSaveJson;
import com.piano.moguyun.utils.DataUt;
import com.piano.moguyun.utils.HttpPostData;
import com.piano.moguyun.utils.LogUtils;
import com.piano.moguyun.utils.ToastUtils;

import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity {

    private EditText etName, etPwd;
    private Button btLog;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        etName = findViewById(R.id.etName);
        etPwd = findViewById(R.id.etPwd);
        btLog = findViewById(R.id.btLog);
        btLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userLogin();
            }
        });
    }

    private LoginBean loginBean;

    private void userLogin() {
        String str = "{\"password\":\"" + etPwd.getText().toString().trim() + "\",\n" +
                "\t\"loginType\":\"android\",\n" +
                "\t\"uuid\":\"\",\n" +
                "\t\"phone\":\"" + etName.getText().toString().trim() + "\"\n" +
                "}";
        RequestBody body = RequestBody.create(DataUt.JSON, str);
        OkGo.<String>post(DataUt.loginUri)
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loginBean = new Gson().fromJson(response.body(), LoginBean.class);
                        if (loginBean != null) {
                            int code = loginBean.getCode();
                            if (code == 200 && loginBean.getData() != null) {
                                //
                                if (loginBean.getData().getRoleGroup() != null && loginBean.getData().getRoleGroup().size() > 0){
                                    String sign = HttpPostData.getPlanByStuSign(loginBean.getData().getUserId(),loginBean.getData().getRoleGroup().get(0).getRoleKey());
                                    getPlanByStu(loginBean.getData().getToken(),sign);//登录成功获取学院信息
                                }else{
                                    ToastUtils.toast("身份信息为空", 2);
                                }
                            }else{
                                ToastUtils.toast(loginBean.getMsg(), 2);
                            }
                        }
                    }
                });
    }



    /**
     * 获取学院信息
     */
    private void getPlanByStu(String token,String sign){
        String str = "{\n" +
                "\t\"state\":\"\"\n" +
                "}";
        RequestBody body = RequestBody.create(DataUt.JSON, str);
        OkGo.<String>post(DataUt.getPlanByStu)
                .upRequestBody(body)
                .headers("Host",DataUt.Host)
                .headers("Accept-Language",DataUt.Accept_Language)
                .headers("User-Agent",DataUt.user_agent_value)
                .headers("roleKey","student")
                .headers("Sign",sign)
                .headers("Authorization",token)
                .headers("Content-Type",DataUt.Content_Type)
                .headers("Accept-Encoding","")
                .headers("Cache-Control",DataUt.Cache_Control)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("TAG","学院信息--------->"+response.body());
                        PlanByStuBean planByStuBean = new Gson().fromJson(response.body(), PlanByStuBean.class);
                        if (planByStuBean != null) {
                            int code = planByStuBean.getCode();
                            if (code == 200 && planByStuBean.getData() != null) {

                                List<PlanByStuBean.DataBean> dataBeanList = planByStuBean.getData();
                                if (dataBeanList != null && dataBeanList.size() > 0){
                                    Intent intent = new Intent(MainActivity.this, SignAct.class);
                                    intent.putExtra("token",loginBean.getData().getToken());
                                    intent.putExtra("userId",loginBean.getData().getUserId());
                                    intent.putExtra("planId",dataBeanList.get(0).getPlanId());
                                    startActivity(intent);
                                    ToastUtils.toast("登录成功", 1);
                                }else{
                                    ToastUtils.toast("学院信息为空", 2);
                                }
                            }else{
                                ToastUtils.toast(loginBean.getMsg(), 2);
                            }
                        }
                    }
                });
    }

}